import { createAction } from 'redux-actions'
import util from "./util"
// import { postJson, check, token, parseJSON, checkStatus, data } from './util'
import { getToken, check, body, delJson, getJson, postJson, putJson, parseJSON, checkStatus, data, token } from './util'
import fetch from 'isomorphic-fetch'

// 上传文件到文件服务器
export const uploadFile = createAction('upload file', ({url, file, _uuid, meta}) =>{
	if(url.indexOf("aliyun") > -1){
		return (
			fetch(url, {
				method: 'put',
				body: file,
				headers: {
					'Content-Type': ''
				}
			})
			.then(checkStatus)
			.then(res => ({res, _uuid, meta}))
		)
	}else{
		let data = new FormData();
		data.append('file', file);
		return(
			fetch(url, {
				...postJson,
				headers: {
				},
				body: data,
			})
			.then(checkStatus)
			.then(res => ({res, _uuid, meta}))
		)
	}
})


// 上传文件到文件服务器
export const fileUpload = createAction('file upload', ({file, url, cb}) => {
	if(url.indexOf("aliyun") > -1){
		return (
			fetch(url, {
				method: 'put',
				headers: {
					'Content-Type': ' ',
				},
				body: file,
			})
			.then(checkStatus)
			.then(() => {
				cb()
			})
		)
	}else{
		let data = new FormData();
		data.append('file', file);
		return (
			fetch(url, {
				...postJson,
				headers: {
				},
				body: data,
			})
			.then(checkStatus)
			.then(() => {
				cb()
			})
		)
	}
})

export const doneUploadFile = createAction('done upload file')
